Overlaying Data#
Overlaying a Waveform#
The most simple example of overlaying is using the waveform itself, we can get the waveform with torchaudio:
import torchaudio
wav, sr = torchaudio.load('https://www2.cs.uic.edu/~i101/SoundFiles/CantinaBand3.wav')
To overlay the waveform, just add it as the second argument to the wv.Audio function:
import waloviz as wv
wv.extension()
wv.Audio('https://www2.cs.uic.edu/~i101/SoundFiles/CantinaBand3.wav', wav, minimal=True)
wv.Audio((wav, sr), wav, minimal=True)
wv.Audio((wav, sr), over_curve=wav, minimal=True)
# All of the above create the exact same player, only the last one is displayed
You can see the range of waveform values on the right side vertical axis, to hide the waveform tap the little blue line icon on the bottom right side.
In order to overlay more than one curve, you can add a list of curves, let’s add the waveform envelope:
import torch
envelope = torch.nn.functional.max_pool1d(wav, kernel_size=101, stride=50, padding=50)
wv.Audio((wav, sr), [wav, envelope], minimal=True)
If you’ll zoom in, you might notice that the orange curve does not envelope the blue curve exactly, this is because WaloViz skips values by default when given very long overlays, such as waveforms. This behavior keeps the player responsive and avoids unexpected errors.
If you need WaloViz to avoid skipping values, you can set themax_sizeparameter to do so.
The exact same thing can be done with the over curves being functions that receive the wav and sr:
def wav_curve(wav, sr):
return wav
def envelope_curve(wav, sr):
return torch.nn.functional.max_pool1d(wav, kernel_size=101, stride=50, padding=50)
wv.Audio('https://www2.cs.uic.edu/~i101/SoundFiles/CantinaBand3.wav', [wav_curve, envelope_curve], minimal=True)
You can also control the curve names and colors like so:
wv.Audio(
(wav, sr),
{"wav": wav, "envelope": envelope},
over_curve_colors={"wav": "#CCCC11", "envelope": "#22AA55"},
minimal=True
)
When you overlay a stereo audio, if the curve has only one channel it is duplicated, if it has two channels it is overlayed accordingly:
url = 'https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Samples/AFsp/M1F1-Alaw-AFsp.wav'
def one_channel(wav, sr):
return wav.sum(dim=0).abs()
def two_channels(wav, sr):
return -wav.abs()
wv.Audio(url, {"1": one_channel, "2": two_channels}, minimal=True)